Skip to content

[BUGF] Fix SequentialWorkflow initialization validation - #1212

Closed
Steve-Dusty wants to merge 3 commits into
kyegomez:masterfrom
Steve-Dusty:master
Closed

[BUGF] Fix SequentialWorkflow initialization validation#1212
Steve-Dusty wants to merge 3 commits into
kyegomez:masterfrom
Steve-Dusty:master

Conversation

@Steve-Dusty

@Steve-Dusty Steve-Dusty commented Nov 19, 2025

Copy link
Copy Markdown
Contributor

Problem

SequentialWorkflow was validating agent requirements during init, causing initialization to fail when no agents
were provided. This broke existing tests and prevented valid use cases where users want to instantiate an empty
workflow for later configuration.

Error:
workflow = SequentialWorkflow() # Failed with ValueError

ValueError: Agents list cannot be None or empty

Location: swarms/structs/sequential_workflow.py:104

Root Cause

The reliability_check() method was called in init (line 83), immediately validating that agents exist:

def init(self, ...):
# ... set attributes ...
self.reliability_check() # ← Validation during init
self.flow = self.sequential_flow()
self.agent_rearrange = AgentRearrange(...)

def reliability_check(self):
if self.agents is None or len(self.agents) == 0:
raise ValueError("Agents list cannot be None or empty") # ← Failed here

This violated the principle of separating object construction from usage validation.

Solution

Deferred validation pattern: Allow initialization without agents, validate only when execution methods are called.

Changes Made

  1. Core Fix - swarms/structs/sequential_workflow.py

Modified init (lines 83-99):

Only validate and initialize if agents are provided

if self.agents is not None and len(self.agents) > 0:
self.reliability_check()
self.flow = self.sequential_flow()
self.agent_rearrange = AgentRearrange(...)
else:
self.flow = ""
self.agent_rearrange = None

Added validation to all execution methods:

  • run() - line 191
  • run_batched() - line 238
  • run_async() - line 273
  • run_concurrent() - line 304

def run(self, task: str, ...):
if self.agents is None or len(self.agents) == 0:
raise ValueError("Agents list cannot be None or empty. Add agents before running the workflow.")

  if self.agent_rearrange is None:
      raise ValueError("Workflow not properly initialized. AgentRearrange is None.")

  # ... execution logic
  1. Test Updates - test_sequential_workflow.py

Fixed test_sequential_workflow_initialization (lines 7-15):
def test_sequential_workflow_initialization():
workflow = SequentialWorkflow()
assert isinstance(workflow, SequentialWorkflow)
assert workflow.agents is None # ← Was checking workflow.tasks
assert workflow.max_loops == 1
assert workflow.flow == ""
assert workflow.agent_rearrange is None

Updated test_sequential_workflow_error_handling (lines 235-264):
Changed from expecting errors at init to expecting errors at execution:

Initialization is now allowed

workflow_none = SequentialWorkflow(agents=None)
assert workflow_none.agents is None

Error raised when trying to run

with pytest.raises(ValueError, match="Agents list cannot be None or empty"):
workflow_none.run("test task")

  1. Documentation Updates - docs/swarms/structs/sequential_workflow.md

Updated parameter table (line 306):

  • | agents | List of agents to execute in sequence | Required |
  • | agents | List of agents to execute in sequence | None (optional at init, required before run) |

Added new sections:

  • "Initialization Behavior" - explains both patterns (with/without agents)
  • Enhanced "Error Handling" - shows validation at execution time
  • Added Raises: documentation to all execution methods

Updated init documentation:

  • Clarified agents can be None during initialization
  • Added note about state when initialized without agents

Testing

All tests pass: ✅ 8 passed, 2 skipped

$ pytest test_sequential_workflow.py -v
test_sequential_workflow_initialization PASSED
test_sequential_workflow_initialization_with_agents PASSED
test_sequential_workflow_multi_agent_execution PASSED
test_sequential_workflow_batched_execution PASSED
test_sequential_workflow_with_multi_agent_collaboration PASSED
test_sequential_workflow_error_handling PASSED
test_sequential_workflow_agent_names_extraction PASSED
test_sequential_workflow_team_awareness PASSED

Test coverage:

  • ✅ Empty initialization works
  • ✅ Initialization with agents works
  • ✅ Running without agents raises clear error
  • ✅ All execution methods validate before running
  • ✅ Existing workflows continue to work

Impact

Breaking Changes

None. All existing code continues to work because:

  • Workflows initialized with agents behave identically
  • All examples in docs/tests use the standard pattern: SequentialWorkflow(agents=[...])

New Behavior

Users can now:

Create empty workflow

workflow = SequentialWorkflow()

Configure later

workflow = SequentialWorkflow(agents=[agent1, agent2])
workflow.run("task")

Attempting to run without agents gives clear error:
ValueError: Agents list cannot be None or empty. Add agents before running the workflow.

Benefits

  1. ✅ Follows standard initialization patterns (separate construction from validation)
  2. ✅ More flexible API (deferred configuration possible)
  3. ✅ Better error messages (explains what to do)
  4. ✅ Tests pass without workarounds
  5. ✅ No breaking changes to existing code

Files Changed

File Lines Changed Purpose
swarms/structs/sequential_workflow.py ~40 Core validation fix
test_sequential_workflow.py ~30 Update tests for new behavior
docs/swarms/structs/sequential_workflow.md ~60 Document new initialization pattern

Checklist

  • Fix core validation issue
  • Add validation to all execution methods
  • Update failing tests
  • Run full test suite (8/8 passed)
  • Update documentation
  • Add error handling examples
  • Verify no breaking changes

📚 Documentation preview 📚: https://swarms--1212.org.readthedocs.build/en/1212/

@github-actions github-actions Bot added documentation Improvements or additions to documentation tests structs labels Nov 19, 2025
@Steve-Dusty Steve-Dusty changed the title Fix SequentialWorkflow initialization validation [BUGF] Fix SequentialWorkflow initialization validation Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation structs tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant